# ----------------------------------------------------------------------------
#   ERCopyMoveTool Change Move/Copy tool to Copy/Move tool
# ----------------------------------------------------------------------------
#   This script changes the behavior of the Move tool such that
#   its primary function is to Copy (without the Ctrl key), but
#   Move when the Ctrl key is depressed.
#
require "sketchup.rb"
require "EventRelay.dll"
# ----------------------------------------------------------------------------
class ERCopyMoveTool < EventRelay
# ----------------------------------------------------------------------------

    SUPPRESS = true
    ALLOW = false

    # ----------------------------------
    #    initialize
    # ----------------------------------
    def initialize
        super
        @toolsObserver = MyToolsObserver.new
        Sketchup.active_model.tools.add_observer(@toolsObserver)
        #-SendKeyMacro("!WR") #// *Testing* Open Ruby Console
    end
    # ----------------------------------
    #    Mouse OnLButtonDown
    # ----------------------------------
    def OnLButtonDown(flags, x, y)
        #-puts "OnLButton DOWN"
        toolis = @toolsObserver.getActiveTool()
		#-puts("ERCopyMove::OnLButtonDown ToolsObserver:#{toolis}")

        # MoveTool gets a Ctrl key to cause Copy mode
        if ( (toolis == "MoveTool") && (@toolsObserver.getToolState() == 0) )
            #-puts("ERCopyMove:OnLButtonDown Sending {LCTRL}")
            SendKeyMacro("{LCTRL}") # put MoveTool in Copy mode
        end
        return ALLOW
    end
    # ----------------------------------
    #    Menus
    # ----------------------------------
    def OnMenuSelected(menuId, menuLabel)
        #-puts "ERCopyMove:OnMenuSelected MenuId:#{menuId} label:#{menuLabel}"
        # Make sure the tools observer is re-instated across file new/open/save as
        @toolsObserver.checkToolsObserver()
        return ALLOW
    end #OnMenuSelected

end # class ERCopyMoveTool
# ----------------------------------------------------------------------------
class MyToolsObserver < Sketchup::ToolsObserver
# ----------------------------------------------------------------------------
    # We need a Tools Observer to tell us when the MoveTool
    # is active. We need its state to tell us when an entity is
    # stuck to its cursor.

    def initialize
        super
        @currentModelId = Sketchup.active_model.object_id
        @currentTool = "notool"
        @currentToolState = 0
    end

    def onActiveToolChanged(tools_object, toolname, toolid)
        @currentTool = toolname
        #-puts "Active Tool Changed: #{@currentTool}"
    end
    def onToolStateChanged(tools_object, toolname, toolid, state)
        @currentTool = toolname
        @currentToolState = state
        #-puts "Tool State Changed: #{@currentTool} #{state}"
    end

    def checkToolsObserver
        if ( @currentModelId != Sketchup.active_model.object_id)
            #-printf("old[%d] new[%d]\n", @currentModelId, Sketchup.active_model.object_id)
            Sketchup.active_model.tools.remove_observer(self) #// probably not necessary
            @currentModelId = Sketchup.active_model.object_id
            Sketchup.active_model.tools.add_observer(self)
        end
    end

    def getActiveTool
        return @currentTool
    end

    def getToolState
        return @currentToolState
    end

end # class MyToolsObserver
# ----------------------------------------------------------------------------

unless file_loaded?("ERCopyMoveTool.rb")
    myCopyMoveTool = ERCopyMoveTool.new
    file_loaded "ERCopyMoveTool.rb"
end

